package com.android.nextBus.Activities;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

import com.android.nextBus.DB.RouteItem;
import com.android.nextBus.DB.SQLiteOperationsWrapper;


public class ViewRoutesActivity extends ListActivity {
	
	public static final String ROUTE_ID_KEY = "route_id";
	public static final String ROUTE_NAME_KEY = "route_name";
	ArrayList<RouteItem> routes;
	SQLiteOperationsWrapper db;
	int[] routeIDs;
	String[] routeNames;
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.routes_layout);

		db = new SQLiteOperationsWrapper(this);
		db.open();
		
		routes = db.getAllRouteItems();
		
		routeIDs = new int[routes.size()];
		routeNames = new String[routes.size()];

		
		for(int i = 0; i < routes.size(); i++) {
			routeIDs[i] = routes.get(i).getRouteID();
			routeNames[i] = routes.get(i).getRouteShortName() + ": " + routes.get(i).getRouteLongName();
		}
		
		ListAdapter mAdapter = new ArrayAdapter<String>(this, R.layout.route_row, routeNames);
		setListAdapter(mAdapter);
		
		
		getListView().setOnItemClickListener(new OnItemClickListener() {
		
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				// TODO Auto-generated method stub				
				Intent intent = new Intent(getApplicationContext(), ViewStopsActivity.class);
				intent.putExtra(ROUTE_ID_KEY, routeIDs[position]);
				intent.putExtra(ROUTE_NAME_KEY, routeNames[position]);
				startActivity(intent);
			}
		});
	}

}
